package com.lemon.boot.common.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.lemon.boot.common.consts.ApiConst;
import com.lemon.boot.common.handler.MultiTenantHandler;
import com.lemon.boot.common.properties.TenantConfigProperties;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 李猛
 * @datetime 2023/10/7 15:03
 * @description 类对象
 */
@Configuration
@MapperScan(value = ApiConst.SYSTEM_PACKAGE + ".dao")
@AutoConfigureAfter(TenantConfigProperties.class)
public class MybatisPlusConfiguration {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(final TenantConfigProperties properties) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //乐观锁配置
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

        //多租户配置
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new MultiTenantHandler(properties)));

        //分页配置
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}
